package paper.xml;

import javax.xml.parsers.*;
import org.xml.sax.helpers.*;
import org.xml.sax .*;
import java.io.*;
import org.xml.sax.helpers.DefaultHandler;
import java.util.*;

import paper.xml.PaperSet;


class CiteseerParser extends DefaultHandler
{
    static final int NONE=0,IDENTIFIER=1, TITLE=2, CITES=3, CITEDBY=4;
    int where;
    PaperSet theSet;
    Vector<Integer> cites,citedBy;
    Vector<String> authors;
    CiteseerParser(PaperSet set)
    {
        theSet = set;
        where = NONE;
        authors = new Vector<String>();
        cites = new Vector<Integer>();
        citedBy = new Vector<Integer>();
    }
    public void startElement(String uri, String localName, String qName, Attributes attributes) 
    {
        //System.out.print("Handler: uri=" + uri + " localName=" + localName + " qName=" + qName + " atts=");
        int i;
        //for(i=0; i< attributes.getLength(); i++)
        //{
        //    System.out.print( attributes.getQName(i) + "=" + attributes.getValue(i) );
        //}
        //System.out.println(" ");
        if( qName.equals("identifier") )
            where = IDENTIFIER;
        else if( qName.equals("dc:title") )
            where = TITLE;
        else if( qName.equals("oai_citeseer:relation" ) )
        {
            if(attributes.getValue("type").equals("References"))
                where = CITES;
            if(attributes.getValue("type").equals("Is Referenced By"))
                where = CITEDBY;
        }
        else if( qName.equals("oai_citeseer:author") )
        {
            authors.add( attributes.getValue("name") );
        }
    }
    public void characters(char[] ch, int start, int length) 
    {
        String raw = new String(ch,start,length);
        if( where==IDENTIFIER && length > 16)
        {
            String val = new String(ch,start+16,length-16);
            theSet.setCurrentId(Integer.parseInt(val));
            where = NONE;
        }
        else if( where==TITLE )
        {
            theSet.setCurrentName(raw);
            where = NONE;
        }
        else if( where==CITES && length>16)
        {
            //System.out.println("Cite: " + start + "," + length + " :" + raw);
            String val = new String(ch,start+16,length-16);
            //System.out.println("New Cite: " + val);
            cites.add( new Integer(val) );
            where = NONE;
        }
        else if( where==CITEDBY && length>16)
        {
            //System.out.println("Cite: " + start + "," + length + " :" + raw);
            String val = new String(ch,start+16,length-16);
            //System.out.println("New Cite: " + val);
            citedBy.add( new Integer(val) );
            where = NONE;
        }
    
    }
    public void endElement(String uri, String localName, String qName) 
    {
        if( qName.equals("record") )
        {
            theSet.finalise(cites,citedBy,authors);   
            cites.clear();
            citedBy.clear();
            authors.clear();
        }
    }
}
